// pages/index/organism/organism.js
import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog"

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    theme: {},
    //当前选中标签页
    tabIndex: 0,
    /**
     * ZZ 代表自治组织
     * SH 代表社会组织
     */
    ZZList: [],
    SHList: [],
    //是否刷新
    isZZRefresh: false,
    isSHRefresh: false,
    //是否加载中
    isLoading: true,
    //分页
    pageZZNumber: 1,
    pageSHNumber: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      theme: app.globalData.theme
    })

    this.getZZList()
    this.getSHList()
  },

  /**
   * 标签栏切换
   * @param e
   */
  onTabChange:function (e) {
    this.setData({
      tabIndex: e.detail.index
    })
  },

  /**
   * 自治组织刷新
   */
  onZZRefresh: function (e) {
    this.data.pageZZNumber = 1
    this.setData({
      isZZRefresh: true,
      ZZList: [],
    })
    this.getZZList()
  },

  /**
   * 自治组织山上拉加载
   */
  onZZLower: function (e) {
    this.data.pageZZNumber++
    this.getZZList()
    Toast.loading({
      duration: 0,
      message: "正在加载..."
    })
  },

  /**
   * 获取自治组织数据
   */
  getZZList: function () {
    this.setData({
      isLoading: true
    })
    http.get("voluntary_association/getListPage",{
      page_number: this.data.pageZZNumber,
      page_size: 10,
      area_code: app.globalData.areaCode
    }).then(res => {
      Toast.clear()
      if (res.data.code === http.REQUEST_OK) {
        if (res.data.data.list.length > 0) {
          let list = this.data.ZZList
          res.data.data.list.forEach(item => {
            list.push(item)
          })
          this.setData({
            ZZList: list
          })
        }else {
          if (this.data.pageZZNumber > 1) {
            Toast.fail("没有更多数据了")
          }
          this.data.pageZZNumber > 1 ? this.data.pageZZNumber-- : 1
        }
      }else {
        this.data.pageZZNumber > 1 ? this.data.pageZZNumber-- : 1
        Toast.fail(res.data.msg)
      }
      this.setData({
        isLoading: false,
        isZZRefresh: false
      })
    }, err => {
      Toast.clear()
      this.setData({
        isLoading: false,
        isZZRefresh: false
      })
      this.data.pageZZNumber > 1 ? this.data.pageZZNumber-- : 1
      Toast.fail("网络异常，请稍后再试")
    })
  },


  /**
   * 社会组织刷新
   */
  onSHRefresh: function (e) {
    this.setData({
      isSHRefresh: true,
      SHList: []
    })
    this.getSHList()
  },

  /**
   * 获取社会组织数据
   */
  getSHList: function () {
    this.setData({
      isLoading: true
    })
    http.get("party/getPartyOrgList",{
      zz_type: "D0060_7",
      area_code: app.globalData.areaCode
    }).then(res => {
      if (res.data.code === http.REQUEST_OK) {
        this.setData({
          SHList: res.data.data
        })
      }else {
        Toast.fail(res.data.msg)
      }
      this.setData({
        isLoading: false,
        isSHRefresh: false
      })
    }, err => {
      this.setData({
        isLoading: false,
        isSHRefresh: false
      })
      Toast.fail("网络异常，请稍后再试")
    })
  },

  /**
   * 自治组织成员列表
   */
  onZZPeople: function (e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "./people_list/people_list?type=1&id=" + id
    })
  },

  /**
   * 社会组织成员列表
   */
  onSHPeople: function (e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "./people_list/people_list?type=2&id=" + id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})